<?php
session_start();
include("check.php");
/*include("check2.php");*/
include_once("../function.php");
include_once("../class/email_class.php");
include_once("../class/member_class.php");
include_once("../class/bonus_class.php");
include_once("../class/ulevel_class.php");
header("Content-Type: text/html;charset=utf-8");
#搜索商品
if ($_POST['Search']){
    $SearchContent=$_POST['SearchContent'];
    if ($SearchContent!=NULL){
        $SearchType=$_POST['SearchType'];
        if ($SearchType==1){
            #搜索商品名称
            $_SESSION['Search']="and goodsname like '%".$SearchContent."%'";
        }
    }else{
        $_SESSION['Search']=NULL;
    }
}else{
    if ($_GET['page']==NULL){
        $_SESSION['Search']=NULL;
        $_SESSION['SearchTime']=NULL;
    }
}

$member=getMemberbyID($_SESSION['ID']);

if($_POST['button3']){
    if($member['ispay']==0){
        alert("会员未激活，无法购物","?");
        return;
    }
    if($_POST['UID']==""){
        alert("请选择商品","?");
        return;
    }else{
        foreach($_POST['UID'] as $v){

            $rs=getOne("select * from goods where id={$v}");
            if($rs['zhekou']>0){
                $arr[$rs['id']]=array("id"=>$rs['id'],"goodsname"=>$rs['goodsname'],"num"=>$_POST[$rs['id']."num"],"price"=>$rs['price']/10*$rs['zhekou'],"zhekou"=>$rs['zhekou'],"yuanjia"=>$rs['price']);
            }else{
                $arr[$rs['id']]=array("id"=>$rs['id'],"goodsname"=>$rs['goodsname'],"num"=>$_POST[$rs['id']."num"],"price"=>$rs['price'],"zhekou"=>$rs['zhekou'],"yuanjia"=>$rs['price']);
            }
            if($_POST[$rs['id']."num"]==0){//是否输入商品数量
                alert("请输入商品数量","?");
                return;
            }elseif($rs['kucun']<$_POST[$rs['id']."num"]){
                alert("商品库存不足请与管理员联系","?");
                return;
            }else{
//                if($_POST['fangshi'.$rs['id']]==""){//是否选择结算方式
//                    alert("请选择结算方式","?");
//                    return;
//                }else{

                    $_POST['fangshi']=1;

                    $price=$rs['price']*$_POST[$rs['id']."num"];//商品数量的总价格
                    $ulevel_cl=new ulevel_class();

                    $iul=$ulevel_cl->getulevelbyulevel($member['ulevel']);
                    if($rs['zhekou']>0){
                        if($_POST['fangshi'.$rs['id']]==1){//电子币结算
                            $dianzi+=$price/10*$rs['zhekou'];
                            $daijin+=0;
                            if($member['mey']<$dianzi){
                                alert("金额不足，请充值","?");
                                return;
                            }
                        }
                    }
                    else{
                        $ismey=0;


                        if($_POST['fangshi']==1){//电子币结算
                            $dianzi+=$price;
                            $daijin+=0;
                            if($member['mey']<$dianzi){
                                alert("您的余额不足，请先充值","?");
                                return;
                            }
                        }elseif($_POST['fangshi'.$rs['id']]==2){//电子币+代金券结算
                        $dianzi+=$price;
                        $daijin+=0;
                        if($member['cfxf']<$dianzi){
                            alert("积分不足2","?");
                            return;
                        }
                    }else{
                            $dianzi+=$price;
                            $daijin+=0;
                            if($member['gwb']<$daijin){
                                alert("积分不足","?");
                                return;
                            }
                        }
                    }

//                }
            }
        }
        foreach($_POST['UID'] as $vf){
            $rs=getOne("select * from goods where id={$vf}");
            $goods_update['kucun']=$rs['kucun']-$_POST[$rs['id']."num"];
            $goods_update['sales']=$rs['sales']+$_POST[$rs['id']."num"];
            edit_update_cl('goods',$goods_update,$rs['id']);
        }
        if( $_POST['fangshi']==1){
            $member_update['mey'] = $member['mey'] - $dianzi;
        }
        /*        $bonus_cl=new bonus_class();
                $bonus_cl->b4bonus($member['id'],$dianzi);
                $bonus_cl->b0bonus();*/
        $lx=1;
        $FileID=store($member['id'],$arr,$member,$member_update,$daijin,$dianzi,$lx,0);
        /*		$_email=new email_class();
                $email=$_email->getemail();
                if ($email['ddtzadmin']==1){
                    $title="会员订单";
                    $content="管理员您好,会员".$member['nickname']."于".now()."向公司购买产品,订单编号：".$FileID."";
                    $_email->sendemail($email['emailuser'],$email['emailname'],$title,$content);
                }*/

        alert("购买成功","?");
        //echo "<script type='text/javascript'>window.parent.location.reload();</script>";
    }
}



?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <!--<link href="css/style.css" rel="stylesheet" type="text/css" />
    <link href="css/style1.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="./css/iconfont.css"/>
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script src="./js/style.js" type="text/javascript" charset="utf-8"></script>
    <link href="../layui-master/dist/css/layui.css" rel="stylesheet" type="text/css">-->
    <!--标准mui.css-->
    <link rel="stylesheet" href="css/mui.min.css">
    <!--App自定义的css-->
    <link rel="stylesheet" type="text/css" href="css/app.css" />
    <link rel="stylesheet" type="text/css" href="css/icons-extra.css">
    <!--自定义iconfont-->
    <link rel="stylesheet" type="text/css" href="css/iconfont.css">
    <link rel="stylesheet" type="text/css" href="css/shopping-cart.css">
    <script src="../layui-master/dist/layui.js"></script>
    <title>购物商城</title>
    <SCRIPT LANGUAGE=javascript>

        function jiage(numname,pic){
            if (numname.value=="")
            {

                return false;
            }
            if (!(/(^[1-9]\d*$)/.test(numname.value))) {
                mui.alert('输入的不是正整数');
                return false;
            }


            var price=document.getElementById(pic).value;
            var zong=numname.value*price;
            document.getElementById(pic+"zong").innerText=zong;
            zongjiage=0;
            for (var i=0;i<document.form1.UID.length;i++) {
                var e=document.form1.UID[i];
                if (e.checked==true){
                    var jiage=document.getElementById(e.value).value;
                    var shuangliang=document.getElementById(e.value+"num").value;
                    zongjiage=zongjiage+jiage*shuangliang;
                }
            }
            document.getElementById("zongjiage").innerText=zongjiage;
        }

        function check(objName){
            var o=document.getElementsByName(objName);
            for(i=0;i<o.length;i++)if(o[i].checked)return;
            mui.alert("请选择一种商品");
        }

        function zjiage(){
            zongjiage=0;
            for (var i=0;i<document.form1.UID.length;i++) {
                var e=document.form1.UID[i];
                if (e.checked==true){
                    var jiage=document.getElementById(e.value).value;
                    var shuangliang=document.getElementById(e.value+"num").value;
                    zongjiage=zongjiage+jiage*shuangliang;
                }
            }
            document.getElementById("zongjiage").innerText=zongjiage;
        }
    </script>
    <style type="text/css">
        *{margin:0; padding:0.0rem; font-size:0.6rem;}

        .ziti{width:100%; height:1rem; clear:both;}
        .dis{display:none;}
    </style>
</head>
<body>
<header class="mui-bar mui-bar-nav" style="background: #ff8400;">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" style="color: #fff;"></a>
    <h1 class="mui-title" style="color: #fff;">购物车</h1>
</header>
<div style="height:44px;"></div>
<form name="form1" method="post" action="?">
   <!--<div class="ziti" style="height: 3rem;text-align: center;">
       <input type="text" name="SearchContent" style="width:50%;height: 2rem;" placeholder="商品名称" id="SearchContent">
       <input type="submit" name="Search" id="Search" style="width:15%;height: 2rem;" class="layui-btn layui-btn-sm" value="搜  索">
   </div>-->
    <div class="commodity_list_box">
        <div class="commodity_box">
            <div class="commodity_list">
                <ul class="commodity_list_term">
                <?php
//                $pagesize = 100; //设置每页记录数
//                $sql = "SELECT * FROM `goods` WHERE isdisplay=1 ".$_SESSION['Search']." ".$_SESSION['SearchTime']."";
//
//                if($query = mysql_query($sql)){
//                    $sum = mysql_num_rows($query); //计算总记录数
//                }else{
//                    $sum=0;
//                }
//                if($sum % $pagesize == 0) //计算总页数
//                    $total = (int)($sum/$pagesize);
//                else
//                    $total = (int)($sum/$pagesize) + 1;
//                if (isset($_GET['page'])) //获得页码
//                {
//                    $p = (int)$_GET['page'];
//                }
//                else
//                {
//                    $p = 1;
//                }
//                if ($p>$total){
//                    $p=$total;
//                }
//                $start = $pagesize * ($p - 1); //计算起始记录
                //查询该用户加入购物车的商品id
                $uid= $_SESSION['userid'];
                //$sql = 'SELECT gid FROM `car` WHERE uid ='.'$uid"';

                $sql = "SELECT goods.id,goods.goodsimg,goods.price,goods.goodsname FROM  goods INNER JOIN car on goods.id = car.gid WHERE isdisplay=1 and car.uid='$uid'";
                if($query = mysql_query($sql)){
                    while ($row=mysql_fetch_array($query)){
                        ?>
                        <li class="select">
                            <div>
                                <div style="float: left;width: 10%;height: 6rem;text-align: center;padding-top:2.5rem;"><input type="checkbox" name="UID[]" id="UID" value="<?=$row['id']?>" onClick="zjiage();"></div>
                                <img src="../upload/<?=$row['goodsimg']?>" />
                                <div class="div_center" style="float: left;width:40%;margin-left: 2rem;">
                                    <h4><?=$row['goodsname']?></h4>
                                    <p class="now_value"><i>￥</i><b class="qu_su"><?=$row['price']?></b></p>
                                    <span>购买数量</span>
                                    <input style="height: 1rem; display: inline;width:35%;font-size: 10px;" name="<?=$row['id']?>num" id="<?=$row['id']?>num" type="text" value="1" size="4" onKeyUp="jiage(this,<?=$row['id']?>);">
                                    <input name="<?=$row['id']?>" id="<?=$row['id']?>" type="hidden" value="<?=$row['price']?>">
                                </div>
                                <div style="width: 10%; height: 6rem; float: left;padding-top:2.5rem;">
                                    <span>总价格</span>
                                    <label name="<?=$row['id']?>zong" id="<?=$row['id']?>zong"><?=$row['price']?></label>
                                </div>
                            </div>
                        </li>
                        <?php
                    }
                }
                ?>
                </ul>
            </div>
        </div>
    </div>
    <!--<div style="height:100px;"></div>-->
    <div class="settle_box">
        <ul class="all_check select">
        </ul>
        <ul class="total_amount" style="margin-right: 2rem;">
            <li style="display: flex;">合计：<label name="zongjiage" id="zongjiage">0</label></li>&nbsp;&nbsp;&nbsp;&nbsp;
            <li style="display: flex;">
                 <input name="button3" type="submit" class="layui-btn layui-btn-sm" id="button3" value="购买" onClick=" if(confirm('您确定要购买商品吗?')){this.document.selform.submit();return true;}return false;">
            </li>
        </ul>
    </div>
</form>
<script src="js/mui.min.js"></script>
<script src="js/jquery.min.js"></script>

</body>
</html>